/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemUser.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.poemUser.domain.PoemUser;
import me.zhengjie.poemUser.domain.vo.PoemUserQueryCriteria;
import me.zhengjie.poemUser.service.PoemUserService;
import me.zhengjie.poemmanager.domain.Poem;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hwl
* @date 2024-12-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "用户诗歌管理")
@RequestMapping("/api/poemUser")
public class PoemUserController {

    private final PoemUserService poemUserService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportPoemUser(HttpServletResponse response, PoemUserQueryCriteria criteria) throws IOException {
        poemUserService.download(poemUserService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询poemUser")
    @ApiOperation("查询poemUser")
    @AnonymousAccess
    public ResponseEntity<PageResult<PoemUser>> queryPoemUser(PoemUserQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(poemUserService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value="/paged")
    @Log("分页查询poemUser")
    @ApiOperation("分页查询poemUser")
    @AnonymousAccess
    public ResponseEntity<PageResult<PoemUser>> pagedQueryPoemUser(PoemUserQueryCriteria criteria,
                                                                   @RequestParam(defaultValue = "1") int current,
                                                                   @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(poemUserService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增poemUser")
    @ApiOperation("新增poemUser")
    @AnonymousAccess
    public ResponseEntity<Object> createPoemUser( @RequestBody PoemUser resources){
        poemUserService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改poemUser")
    @ApiOperation("修改poemUser")
    @AnonymousAccess
    public ResponseEntity<Object> updatePoemUser(@Validated @RequestBody PoemUser resources){
        poemUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除poemUser")
    @ApiOperation("删除poemUser")
    @AnonymousAccess
    public ResponseEntity<Object> deletePoemUser(@RequestBody List<Integer> ids) {
        poemUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/my")
    @Log("查询我的poem")
    @ApiOperation("查询我的poem")
    @AnonymousAccess
    public List<Poem> queryMyPoem(@RequestParam String userName){
        return poemUserService.getMyPoem(userName);
    }

    @GetMapping("/getById")
    @Log("根据主键查询用户Poem")
    @ApiOperation("根据主键查询用户Poem")
    @AnonymousAccess
    public PoemUser getPoemById(@RequestParam Integer poemId){

        return poemUserService.getPoemById(poemId);
    }

    @PostMapping("/create")
    @Log("用户创建诗诗歌")
    @ApiOperation("用户创建诗歌")
    @AnonymousAccess
    public Integer createByUser(@RequestBody PoemUser poem){
        return poemUserService.createByUser(poem);
    }

    @GetMapping("/all")
    @Log("查询我的poem")
    @ApiOperation("查询我的poem")
    @AnonymousAccess
    public List<Poem> getALL(){
        return poemUserService.getALl();
    }

    @GetMapping("/favorite")
    @Log("查询我收藏的PoemUser")
    @ApiOperation("查询我收藏的PoemUser")
    @AnonymousAccess
    public List<Poem> queryMyFavoritePoem(@RequestParam String userName){

        return poemUserService.getMyFavoritePoem(userName);
    }
}